package com.android.common.flutter;

import android.content.Context;
import com.android.common.constant.Constant;
import io.flutter.FlutterInjector;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.FlutterEngineCache;
import io.flutter.embedding.engine.dart.DartExecutor;
import io.flutter.embedding.engine.loader.FlutterLoader;

public class FlutterEngineManage {
   public static void initFlutterEngines(Context context){
      FlutterEngine engine = new FlutterEngine(context);
      engine.getNavigationChannel().setInitialRoute(Constant.FLUTTER_AWAKE_BY_NATIVE_PAGE);
      FlutterLoader flutterLoader = FlutterInjector.instance().flutterLoader();
      engine.getDartExecutor().executeDartEntrypoint(new DartExecutor.DartEntrypoint(flutterLoader.findAppBundlePath(),Constant.FLUTTER_CACHE_MAIN));
      FlutterEngineCache.getInstance().put(Constant.FLUTTER_ENGINE_ID,engine);
   }

   public static FlutterEngine getFlutterEngine(String engineId){
      return FlutterEngineCache.getInstance().get(engineId);
   }

   public static void remove(String engineId){
      FlutterEngineCache.getInstance().remove(engineId);
   }

   public static void clear(){
      FlutterEngineCache.getInstance().clear();
   }
}
